const rollup = require('rollup');
const fs = require('fs');
let builds = require('./config').getAllBuilds();
function build (builds) {
    let built = 0
    const total = builds.length
    const next = () => {
      buildEntry(builds[built]).then(() => {
        built++
        if (built < total) {
          next()
        }
      }).catch(logError)
    }
    next()
}
function buildEntry(config) {
    const output = config.output
    const { file, banner } = output
    const isProd = /(min|prod)\.js$/.test(file)
    return rollup.rollup(config)
        .then(bundle => {
          return bundle.generate(output)
      })
      .then(({ output: [{ code }] }) => {
        write(file,code)
      })
}
function write (dest, code, zip) {
    return new Promise((resolve, reject) => {
      function report (extra) {
        resolve()
      }
      fs.writeFile(dest, code, err => {
        if (err) return reject(err)
        // if (zip) {
        //   zlib.gzip(code, (err, zipped) => {
        //     if (err) return reject(err)
        //     report(' (gzipped: ' + getSize(zipped) + ')')
        //   })
        // } else {
          report()
        // }
      })
    })
}
function logError (e) {
    console.log(e)
}
build(builds)